﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FS_2013_GUI
{
    /// <summary>
    /// Логика взаимодействия для DownloadWindow.xaml
    /// </summary>
    public partial class DownloadWindow : Window
    {
        public static string fileNames = null;
        private FSMain obj;

        public DownloadWindow()
        {
            InitializeComponent();
        }

        public DownloadWindow(FSMain obj)
        {
            InitializeComponent();
            this.obj = obj;
            pathBox.Text = Global.cn.receivedPath;
            addItemsToFileBox();
        }

        private void addItemsToFileBox()
        {
            Global.cn.Send("get_file_list");
            while (fileNames == null) { }
            string[] files = fileNames.Split('*');
            for (int i = 1; i < files.Length-1; i++)
                downloadList.Items.Add(new DowloadItem { IsSelected = false, TheText = files[i] });
            fileNames = null;
        }

        private void downloadBtn_Click(object sender, RoutedEventArgs e)
        {
            string result = "";
            int number = 0;
            foreach (DowloadItem d in downloadList.Items)
            {
                if (d.IsSelected)
                {
                    obj.downloadList.Items.Add(d.TheText);
                    result += "*" + d.TheText;
                    number++;
                }
            }
            if (result != "")
            {
                Global.cn.Send("send_files " + result);
                Global.cn.StarteceiveFiles(number);
            }
            CloseWindow();
        }

        private void cancelBtn_Click(object sender, RoutedEventArgs e)
        {
            CloseWindow();
        }

        private void CloseWindow()
        {
            Global.mw = obj;
            this.Close();
            obj.ShowDialog();
        }

        private void browseBtn_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
            fd.ShowDialog();
            pathBox.Text = fd.SelectedPath;
            Global.cn.receivedPath = pathBox.Text;
        }
    }
}

public class DowloadItem
{
    public string TheText { get; set; }
    public bool IsSelected { get; set; }
}
